package com.example.download.activity

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.baselibrary.arouter.ArouterContains
import com.android.baselibrary.base.BaseActivity
import com.example.download.R
import com.example.download.databinding.ActivityDownMainBinding
import permissions.dispatcher.RuntimePermissions
import permissions.dispatcher.NeedsPermission
import android.Manifest
import android.os.Environment
import android.os.FileUtils
import android.os.Handler
import com.android.baselibrary.utils.ToastUtils
import com.example.download.utils.DowLoadUtils
import com.example.download.utils.FileSavaUtils
import com.example.download.utils.PermissionTip
import com.example.download.utils.PermissionType
import kotlinx.coroutines.MainCoroutineDispatcher
import permissions.dispatcher.OnPermissionDenied


@RuntimePermissions
@Route(path = ArouterContains.DOWNLOAD_MODEL)
class DownMainActivity : BaseActivity<ActivityDownMainBinding>(), View.OnClickListener {
   private lateinit var dowLoadUtils: DowLoadUtils
    companion object{
         var path:String = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).absolutePath
        const val url = "https://file.greatwallchinese.com/spath1/2022/03/16/45f2d9b122db4bae8c7de05453faf0c6.mp4"
        const val imageUrl =
            "https://tsp-file-prd-1258960627.cos.ap-beijing.myqcloud.com/files/bt-operation-dashboard/20220315091022%E6%90%9C%E7%8B%97%E6%88%AA%E5%9B%BE20220315091004.jpg"

    }
    override fun getLayoutId(): Int = R.layout.activity_down_main

    override fun initView() {
        PermissionTip.tip(this,supportFragmentManager,PermissionType.WRITE_AND_READ)
      onGetWritePermissionWithPermissionCheck()
    }

    override fun initListener() {
        mBinding.btnMoreSame.setOnClickListener(this)
        mBinding.btnSavePic.setOnClickListener(this)
        mBinding.btnMoreSore.setOnClickListener(this)
        mBinding.btnSaveVideo.setOnClickListener(this)
        mBinding.dloadShortDetail.setCancelListener {

        }
    }

    override fun onClick(p0: View?) {
      when(p0?.id){
          //多个文件同时下载
          R.id.btn_more_same ->{

          }
          //多个文件下载顺序下载
          R.id.btn_more_sore ->{

          }
          //单个照片文件下载保存到相册
          R.id.btn_save_pic ->{
              mBinding.dloadShortDetail.visibility =View.VISIBLE
              dowLoadUtils =   DowLoadUtils(imageUrl, path, onSuccessListener = {
                      file, progress, size ->
                  mBinding.dloadShortDetail.setProgressBar(progress,size)
                  if (progress==100){
                      mBinding.dloadShortDetail.setDownLoadCompleted()
                      FileSavaUtils.saveImage(this,file)
                      ToastUtils.show(this,R.string.minivideo_imagdownload_complete)
                      Handler().postDelayed(
                          {  mBinding.dloadShortDetail.visibility =View.GONE
                          },2000
                      )
                  }

              }, onFailureListener = {

              })
              dowLoadUtils.loadFile()
          }
          //单个视频文件下载保存到相册
          R.id.btn_save_video ->{
              mBinding.dloadShortDetail.visibility =View.VISIBLE
              dowLoadUtils= DowLoadUtils(url, path, onSuccessListener = {
                      file, progress, size ->
                  mBinding.dloadShortDetail.setProgressBar(progress,size)
                  if (progress==100){
                      mBinding.dloadShortDetail.setDownLoadCompleted()
                      FileSavaUtils.saveVideo(this,file)
                      ToastUtils.show(this,R.string.minivideo_download_complete)
                      Handler().postDelayed(
                          {  mBinding.dloadShortDetail.visibility =View.GONE
                          },2000
                      )
                  }

              }, onFailureListener = {

              })
              dowLoadUtils.loadFile()
          }
      }
    }

    @NeedsPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    fun onGetWritePermission() {

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionTip.hide()
        onRequestPermissionsResult(requestCode, grantResults)
    }

    @OnPermissionDenied(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    fun onDeniedWritePermission() {

    }
}